jsx3.Class.defineClass(
	"tibco.admin.monitormanage.general.RefreshHandler",                   // the full name of the class to create
    jsx3.lang.Object, //  extends
	null,  // does not implement an interface
	function(handler) {
		handler.refreshSubject = "com.tibco.admin.monitormanage.general.RefreshHandler.refresh";
		handler.registerSubject = "com.tibco.admin.monitormanage.general.RefreshHandler.register";
		handler.unregisterSubject = "com.tibco.admin.monitormanage.general.RefreshHandler.unregister";
		
		handler.prototype.registeredScreens;
		handler.prototype.registeredObjCache;

		/**
		 *  Init method which creates an instance of the RefreshHandler
		 */
		handler.prototype.init = function() {
			this.jsxsuper();

            tibco.admin.monitormanage.general.PageBusUtils.subscribe(handler.refreshSubject, this.handleRefresh, this);
            tibco.admin.monitormanage.general.PageBusUtils.subscribe(handler.registerSubject, this.handleRegistration, this);
            tibco.admin.monitormanage.general.PageBusUtils.subscribe(handler.unregisterSubject, this.handleUnregistration, this);

            this.registeredScreens = new jsx3.util.List();
            this.registeredObjCache = new jsx3.util.List();
		}
		
		/**
         *  Publishes the message which will invoke refresh on the registered components.
         *  This method was designed to be invoked when the "Refresh" button is clicked.
         *  @param screenIdVal the unique screen id which will be used to determine which registered components should 
         *         have the callback function invoked.
         */
        handler.publishRefreshMessage = function(screenIdVal) {
        	var message = { screenId:screenIdVal };
        	tibco.admin.monitormanage.general.PageBusUtils.publish(handler.refreshSubject, message);
        }
        
        /**
         *  Registers a callback function to be invoked on a callback target when a refresh is invoked for a specific screen
         *  @param callbackTargetVal the callback target to which the callback function will be invoked
         *  @param callbackFunctionVal the callback function which will be invoked on the callback target
         *  @param screenIdVal the callbackFunction will only be invoked if the screen identified by the screen id is displayed
         */
        handler.register = function(callbackTargetVal, callbackFunctionVal, screenIdVal) {
			var message = { callbackTarget:callbackTargetVal, callbackFunction:callbackFunctionVal, screenId:screenIdVal };
			tibco.admin.monitormanage.general.PageBusUtils.publish(handler.registerSubject, message);
        }
        
        /**
         *  Not for public consumption.  This is the callback function for handling the refresh of components.
         */
        handler.prototype.handleRefresh = function(subject, message, refresherObj) {
	    	var screenIdx = refresherObj.registeredScreens.indexOf(message.screenId);
	        if(screenIdx != -1)
	        {
	        	var registeredObjList = refresherObj.registeredObjCache.get(screenIdx);
	        	for(i = 0; i < registeredObjList.size(); i++)
	        	{
	        		var registeredObj = registeredObjList.get(i);
	        		var callbackTarget = registeredObj.callbackTarget;
	        		var callbackFunction = registeredObj.callbackFunction;
	        		callbackFunction.call(callbackTarget);
	        	}
        	}
        }
        
        /**
         *  Not for public consumption.  This is the callback function for handling the registration of components.
         */
        handler.prototype.handleRegistration = function(subject, message, refresherObj) {
        	var registeredObj = handler.createRegisteredObjCache(message);
        	
        	var screenId = message.screenId;
        	var screenIdx = refresherObj.registeredScreens.indexOf(screenId);
        	if(screenIdx == -1)
        	{
        		// there is nothing registered for this screen yet
        		refresherObj.registeredScreens.add(screenId);
        		var registeredObjList = new jsx3.util.List();
        		registeredObjList.add(registeredObj);
        		refresherObj.registeredObjCache.add(registeredObjList);
        	}
        	else
        	{
        		// add the registered obj to the existing registeredObjList
        		var registeredObjList = refresherObj.registeredObjCache.get(screenIdx);
        		registeredObjList.add(registeredObj);
        	}
        }
        
        /**
         *  Not for public consumption.  This is the callback function for handling the unregistration of components.
         */
        handler.prototype.handleUnregistration = function(subject, message, refresherObj) {
        	if(message.unregisterAll)
        	{
        		// The perspective must have been switched so reset the refresh handler
        		refresherObj.registeredScreens.clear();
        		refresherObj.registeredObjCache.clear();
        	}
        }
        
        /**
         *  Not for public consumption.  Creates an internal object representing the registered object
         */
        handler.createRegisteredObjCache = function(message) {
        	var registeredObj = new Object();
        	registeredObj.callbackTarget = message.callbackTarget;
        	registeredObj.callbackFunction = message.callbackFunction;
        	return registeredObj;
        }
        
        /**
         *  Not for public consumption.  This method will be invoked by the M&M framework to unsubscribe all of the 
         *  registered components when the perspective is unloaded.
         */
        handler.unregisterAll = function() {
        	var message = { unregisterAll:true };
        	tibco.admin.monitormanage.general.PageBusUtils.publish(handler.unregisterSubject, message);
        }

	}
);